﻿using DevExpress.XtraEditors;
using DXApplicationTangche.UC.圣同润.merchant;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 广州爱特安为科技.UC.圣同润.客服
{
    public partial class Form导入的发货单信息验证 : DevExpress.XtraEditors.XtraForm
    {
        private List<OrderLogisticsInfoImportVerifyVO> orderLogistics = new List<OrderLogisticsInfoImportVerifyVO>();
        public Form导入的发货单信息验证()
        {
            InitializeComponent();
        }

        private void simpleButton导入发货单Excel_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                DataSet result = ExcelUtil.getExcelDatas(openFile.FileName);
                if (result == null)
                {
                    return;
                }
                DataTable dataTable = result.Tables[0];

                this.orderLogistics.Clear();
                int i = 0;
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    if (i != 0)
                    {
                        OrderLogisticsInfoImportVerifyVO orderLogisticsInfoImport = new OrderLogisticsInfoImportVerifyVO(dataRow);
                        if (!String.IsNullOrEmpty(orderLogisticsInfoImport.delivery_id))
                            this.orderLogistics.Add(orderLogisticsInfoImport);
                    }

                    i++;
                }
                this.gridControl发货单信息验证.DataSource = this.orderLogistics;
                this.gridView发货单信息验证.RefreshData();
                this.gridView发货单信息验证.BestFitColumns();
                gridView发货单信息验证.OptionsBehavior.AutoExpandAllGroups = true;
            }
        }

        private void simpleButton开始验证发货单信息_Click(object sender, EventArgs e)
        {
            if (this.gridView发货单信息验证.DataRowCount <= 0)
            {
                return;
            }
            //  转圈等待开始
            this.splashScreenManager.ShowWaitForm();
            this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中...."); // 标题
            this.splashScreenManager.SetWaitFormDescription("正在初始化....."); // 信息
            //  开始验证
            foreach (OrderLogisticsInfoImportVerifyVO item in this.orderLogistics)
            {
                MerchantLogisticVerificationDTO logisticVerificationDTO = MerchantService.getOrderAddressById(item.order_sn);
                if (!String.IsNullOrEmpty(logisticVerificationDTO.user_address))
                {
                    item.returnAddress = logisticVerificationDTO.user_address;
                    if (!item.returnAddress.Equals(item.user_address))
                    {
                        item.verifyStatus += "|地址变更";
                    }
                }
                if (!String.IsNullOrEmpty(logisticVerificationDTO.delivery_id))
                {
                    item.returnDeliveryId = logisticVerificationDTO.delivery_id;
                    if (!item.returnDeliveryId.Equals(item.delivery_id))
                    {
                        item.verifyStatus += "|物流单号不一致";
                    }
                }
                if (1 == logisticVerificationDTO.refund_status)
                {
                    item.verifyStatus += "|售后中";
                }
                else if (2 == logisticVerificationDTO.refund_status)
                {
                    item.verifyStatus += "|退款已完成";
                }
            }
            this.gridView发货单信息验证.RefreshData();
            this.gridView发货单信息验证.BestFitColumns();
            gridView发货单信息验证.OptionsBehavior.AutoExpandAllGroups = true;
            //  转圈等待结束
            this.splashScreenManager.CloseWaitForm();
            MessageBox.Show("验证结束");
        }
        private void gridView发货单信息验证_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "gridColumnuser_address" || e.Column.Name == "gridColumnreturnAddress")
                {
                    if (this.orderLogistics[e.RowHandle].user_address != this.orderLogistics[e.RowHandle].returnAddress)
                    {
                        e.Appearance.BackColor = Color.CadetBlue;
                    }
                }
            }
            catch (Exception ex) { }
            try
            {
                if (e.Column.Name == "gridColumndelivery_id" || e.Column.Name == "gridColumnreturnDeliveryId")
                {
                    if (this.orderLogistics[e.RowHandle].delivery_id != this.orderLogistics[e.RowHandle].returnDeliveryId)
                    {
                        e.Appearance.BackColor = Color.LightYellow;
                    }
                }
            }
            catch (Exception ex) { }
        }
    }

    public class OrderLogisticsInfoImportVerifyVO
    {

        /// <summary>
        /// 
        /// </summary>
        public string order_sn { get; set; }
        /// <summary>
        /// 王索美
        /// </summary>
        public string real_name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string user_phone { get; set; }
        /// <summary>
        /// 辽宁省 本溪市 溪湖区 彩北彩富路35号楼2单元5楼13号
        /// </summary>
        public string user_address { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int total_num { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTime payTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string remark { get; set; }
        /// <summary>
        /// 泽米咕哒喵咛 辣白菜268g*10袋 共10袋共2680g 保质期6个月 0-4℃冷藏保存（72小时内发货）
        /// </summary>
        public string product_name { get; set; }
        /// <summary>
        /// 未发货
        /// </summary>
        public string orderBaseStatus { get; set; }

        public String delivery_id { get; set; }
        public String delivery_name { get; set; }
        public String returnAddress { get; set; }
        public String returnDeliveryId { get; set; }
        public String verifyStatus { get; set; }

        public OrderLogisticsInfoImportVerifyVO(DataRow dataRow)
        {
            this.order_sn = dataRow[0].ToString();
            this.real_name = dataRow[1].ToString();
            this.user_phone = dataRow[2].ToString();
            this.user_address = dataRow[3].ToString();
            try
            {
                this.total_num = int.Parse(dataRow[4].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                this.payTime = (DateTime)dataRow[5];
            }
            catch (Exception ex)
            {
            }
            this.product_name = dataRow[6].ToString();
            try
            {
                this.delivery_name = dataRow[7].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                this.delivery_id = dataRow[8].ToString();
            }
            catch (Exception ex)
            {
            }
        }
    }
}